home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / AmigaSystem / Scalos / GuiGFXLib / src / guigfx_loading.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  14.4 KB  |  665 lines

  1. /*********************************************************************
  2. ----------------------------------------------------------------------
  3.  
  4.     guigfx_loading
  5.  
  6. ----------------------------------------------------------------------
  7. *********************************************************************/
  8.  
  9. #include <string.h>
  10.  
  11. #include <render/render.h>
  12. #include <render/renderhooks.h>
  13. #include <datatypes/pictureclass.h>
  14. #include <datatypes/datatypes.h>
  15. #include <datatypes/datatypesclass.h>
  16. #include <datatypes/pictureclass.h>
  17. #include <datatypes/pictureclassext.h>
  18. #include <libraries/cybergraphics.h>
  19. #include <utility/tagitem.h>
  20. #include <graphics/gfx.h>
  21. #include <graphics/view.h>
  22. #include <exec/memory.h>
  23.  
  24. #include <proto/dos.h>
  25. #include <proto/render.h>
  26. #include <proto/graphics.h>
  27. #include <proto/exec.h>
  28. #include <proto/utility.h>
  29. #include <proto/datatypes.h>
  30. #include <proto/cybergraphics.h>
  31. #include <proto/dos.h>
  32. #include <proto/intuition.h>
  33.  
  34.  
  35. #include <guigfx/guigfx.h>
  36.  
  37. #include "guigfx_global.h"
  38. #include "guigfx_picture.h"
  39. #include "guigfx_picturemethod.h"
  40.  
  41.  
  42. /*********************************************************************
  43. ----------------------------------------------------------------------
  44.  
  45.     picture = LoadPictureDT ( filename, tags )
  46.  
  47.     GGFX_ErrorCode
  48.     GGFX_HSType
  49.  
  50. ----------------------------------------------------------------------
  51. *********************************************************************/
  52.  
  53. PIC *LoadPictureDT(STRPTR file_name, TAGLIST tags)
  54. {
  55.     if (!DataTypesBase)
  56.     {
  57.         return NULL;
  58.     }
  59.     else
  60.     {
  61.         Object *obj;
  62.         LONG err = 0, *errptr = NULL;
  63.         PIC pic, *p = NULL;
  64.         UBYTE *alphaarray = NULL;
  65.  
  66.         LONG transparencycolor = -1;        /* indicate that a mask is still to be created */
  67.  
  68.     
  69.         TurboFillMem(&pic, sizeof(PIC), 0);
  70.     
  71.         errptr = (LONG *) GetTagData(GGFX_ErrorCode, (ULONG) errptr, tags);
  72.  
  73.     
  74.         if (obj = NewDTObject(file_name, PDTA_DestMode, MODE_V43,
  75.                 DTA_SourceType, DTST_FILE, DTA_GroupID, GID_PICTURE,
  76.                 PDTA_Remap, FALSE, TAG_DONE))
  77.         {
  78.             struct BitMapHeader *bmhd;
  79.             ULONG displayID;
  80.     
  81.             if (GetDTAttrs(obj, PDTA_ModeID, &displayID,
  82.                          PDTA_BitMapHeader, &bmhd, TAG_DONE) == 2)
  83.             {
  84.                 BOOL success = TRUE;            
  85.  
  86.  
  87.                 DB(kprintf("*** GetDTAttrs durchgelaufen. Width: %ld - Height %ld\n",
  88.                     bmhd->bmh_Width, bmhd->bmh_Height));
  89.                 
  90.  
  91.                 DB(kprintf("DisplayID: %x\n",displayID));
  92.             
  93.                 if (displayID == INVALID_ID)
  94.                 {
  95.                     displayID = NULL;
  96.                 }
  97.  
  98.                 #ifdef DEBUG
  99.             
  100.                 if (displayID & HAM)
  101.                 {
  102.                     DB(kprintf("DisplayID ist HAM\n"));
  103.                 }
  104.                 
  105.                 if (displayID & EXTRA_HALFBRITE)
  106.                 {
  107.                     DB(kprintf("DisplayID ist EHB\n"));
  108.                 }
  109.  
  110.                 #endif
  111.  
  112.  
  113.             
  114.                 DB(kprintf("----mask---- %ld\n", GetTagData(GGFX_UseMask, FALSE, tags)));
  115.                 if (GetTagData(GGFX_UseMask, FALSE, tags))
  116.                 {
  117.                     DB(kprintf("----checking 4 mask----\n"));
  118.                     if (bmhd->bmh_Masking != mskNone)
  119.                     {
  120.                         BOOL getmask = FALSE;
  121.                         UBYTE *plane = NULL;
  122.     
  123.                         DB(kprintf("&&& bmh masking (%ld) detected.\n", bmhd->bmh_Masking));
  124.                         
  125.                         if (GetDTAttrs(obj, PDTA_MaskPlane, &plane, TAG_DONE) == 1)
  126.                         {
  127.                             getmask = (plane != NULL);
  128.                         }
  129.                         
  130.                         if (bmhd->bmh_Masking == mskHasTransparentColor && bmhd->bmh_Depth <= 8 && !(displayID & HAM))
  131.                         {
  132.                             getmask = TRUE;
  133.                         }
  134.                         
  135.                         
  136.                         if (getmask)
  137.                         {
  138.                             alphaarray = AllocRenderVec(MemHandler, bmhd->bmh_Width * bmhd->bmh_Height);
  139.                         }
  140.     
  141.                         if (alphaarray)
  142.                         {
  143.                             if (plane)
  144.                             {
  145.                                 int y, x;
  146.                                 UBYTE bitmask, *a = alphaarray;
  147.                                 
  148.                                 for (y = 0; y < bmhd->bmh_Height; ++y)
  149.                                 {
  150.                                     bitmask = 0x80;
  151.                                 
  152.                                     for (x = 0; x < bmhd->bmh_Width; ++x)
  153.                                     {                    
  154.                                         *a++ = (plane[x>>3] & bitmask) ? 255 : 0;
  155.                                 
  156.                                         if (!(bitmask >>= 1))
  157.                                         {
  158.                                             bitmask = 0x80;
  159.                                         }
  160.                                     }
  161.                                     plane += (((bmhd->bmh_Width + 15) >> 4) << 1);
  162.                                 }
  163.                                 DB(kprintf("&&& created alpha-channel from PDTA_MaskPlane.\n"));
  164.                                 success = TRUE;
  165.                             }
  166.                             else
  167.                             {
  168.                                 transparencycolor = bmhd->bmh_Transparent;
  169.                                 DB(kprintf("&&& creating alpha-channel from transparent color %ld later...\n", transparencycolor));
  170.                             }
  171.                         }
  172.                     }
  173.                 }
  174.             
  175.                 if (success)
  176.                 {
  177.                     pic.pixelformat = PIXFMT_CHUNKY_CLUT;
  178.                     pic.width = bmhd->bmh_Width;
  179.                     pic.height = bmhd->bmh_Height;
  180.                     pic.aspectx = bmhd->bmh_XAspect ? bmhd->bmh_XAspect : 1;
  181.                     pic.aspecty = bmhd->bmh_YAspect ? bmhd->bmh_YAspect : 1;
  182.                     pic.hstype = GetTagData(GGFX_HSType, HSTYPE_UNDEFINED, tags);
  183.                     
  184.                     pic.pixelformat = (bmhd->bmh_Depth > 8) ? PIXFMT_0RGB_32 : PIXFMT_CHUNKY_CLUT;
  185.             
  186.                     if (pic.pixelformat == PIXFMT_CHUNKY_CLUT)
  187.                     {
  188.                         if (DoDTMethod(obj, NULL, NULL, DTM_PROCLAYOUT, NULL, TRUE))
  189.                         {
  190.                             struct BitMap *bmap;
  191.                             if (GetDTAttrs(obj, PDTA_BitMap, &bmap, TAG_DONE) == 1)
  192.                             {
  193.                                 if (bmap)
  194.                                 {
  195.                                     switch (pic.pixelformat)
  196.                                     {
  197.                                         case PIXFMT_CHUNKY_CLUT:
  198.                                         {
  199.                                             ULONG *creg;
  200.                                             LONG ncol;
  201.                                             if (GetDTAttrs(obj, PDTA_NumColors, &ncol, PDTA_CRegs, &creg, TAG_DONE) == 2)
  202.                                             {
  203.                                                 if (creg && ncol)
  204.                                                 {
  205.                                                     if (pic.palette = CreatePalette(RND_RMHandler, MemHandler, RND_HSType, 
  206.                                                         pic.hstype == HSTYPE_UNDEFINED ? DEFAULT_PALETTE_HSTYPE : pic.hstype, 
  207.                                                         TAG_DONE))
  208.                                                     {
  209.                                                         ImportPalette(pic.palette, creg, ncol,
  210.                                                             RND_PaletteFormat, PALFMT_RGB32, 
  211.                                                             RND_EHBPalette, displayID & EXTRA_HALFBRITE,
  212.                                                             TAG_DONE);
  213.             
  214.                                                         if (!ReadBitMapArray(bmap, &pic, displayID, NULL))
  215.                                                         {
  216.                                                             err = ERROR_NO_FREE_STORE;
  217.                                                         }
  218.  
  219.                                                         /*
  220.                                                         **    the palette is no longer needed when the picture
  221.                                                         **    is a planar HAM image! we must discard it
  222.                                                         */
  223.  
  224.                                                         if (displayID & HAM)
  225.                                                         {
  226.                                                             if (pic.palette)
  227.                                                             {
  228.                                                                 DeletePalette(pic.palette);
  229.                                                                 pic.palette = NULL;
  230.                                                             }
  231.                                                         }
  232.  
  233.         
  234.                                                     }
  235.                                                     else err = ERROR_NO_FREE_STORE;
  236.                                                 }
  237.                                                 else err = ERROR_REQUIRED_ARG_MISSING;
  238.                                             }
  239.                                             else err = ERROR_REQUIRED_ARG_MISSING;
  240.                                             break;
  241.                                         }
  242.         
  243.                                         case PIXFMT_0RGB_32:
  244.                                         {
  245.                                             if (!ReadBitMapArray(bmap, &pic, displayID, NULL))
  246.                                             {
  247.                                                 err = ERROR_NO_FREE_STORE;
  248.                                             }
  249.                                             break;        
  250.                                         }
  251.                                     }
  252.                                 }
  253.                                 else err = ERROR_NO_FREE_STORE;
  254.                             }
  255.                             else err = ERROR_REQUIRED_ARG_MISSING;
  256.                         }
  257.                         else
  258.                         {
  259.                             if ((err = IoErr()) == 0)
  260.                             {
  261.                                 err = ERROR_NO_FREE_STORE;
  262.                             }
  263.                         }
  264.         
  265.                     }
  266.                     else
  267.                     {
  268.                         DB(kprintf("*** DTM_READPIXELARRAY\n"));
  269.                         if (pic.array = AllocRenderVec(MemHandler, 4 * pic.width * pic.height))
  270.                         {
  271.                             DoMethod(obj, DTM_READPIXELARRAY, pic.array,
  272.                                     RECTFMT_ARGB, pic.width * 4,
  273.                                     0,0, pic.width, pic.height);
  274.                         }
  275.                         else err = ERROR_NO_FREE_STORE;
  276.                     }
  277.                 }
  278.                 else err = ERROR_NO_FREE_STORE;
  279.             }
  280.             else
  281.             {
  282.                 if ((err = IoErr()) == 0)
  283.                 {
  284.                     err = ERROR_NO_FREE_STORE;
  285.                 }
  286.             }
  287.     
  288.             DisposeDTObject(obj);
  289.         }
  290.         else
  291.         {
  292.             if ((err = IoErr()) == 0)
  293.             {
  294.                 err = ERROR_OBJECT_WRONG_TYPE;
  295.             }
  296.         }
  297.     
  298.         if (!err)
  299.         {        
  300.             if (!(p = MakePicture(pic.array, pic.width, pic.height,
  301.                 GGFX_PaletteFormat, PALFMT_PALETTE,
  302.                 GGFX_HSType, pic.hstype,
  303.                 GGFX_PixelFormat, pic.pixelformat,
  304.                 GGFX_AspectX, pic.aspectx,
  305.                 GGFX_AspectY, pic.aspecty,
  306.                 GGFX_Palette, (ULONG)(pic.palette),
  307.                 GGFX_Owner, TRUE,
  308.                 TAG_DONE)))
  309.             {
  310.                 err = ERROR_NO_FREE_STORE;
  311.             }
  312.             else
  313.             {
  314.                 if (alphaarray)
  315.                 {
  316.                     if (transparencycolor > -1 && pic.pixelformat == PIXFMT_CHUNKY_CLUT)
  317.                     {
  318.                         int y, x;
  319.                         UBYTE *p = (UBYTE *) pic.array;
  320.                         UBYTE *a = alphaarray;
  321.                         
  322.                         for (y = 0; y < pic.height; ++y)
  323.                         {
  324.                             for (x = 0; x < pic.width; ++x)
  325.                             {
  326.                                 *a++ = (*p++ == transparencycolor ? 0 : 255);
  327.                             }
  328.                         }
  329.                     
  330.                         DB(kprintf("&&& created alpha channel from transparency color.\n"));
  331.                     }
  332.  
  333.                     if (PIC_SetAlpha(p, alphaarray, pic.width, pic.height, NULL))
  334.                     {
  335.                         DB(kprintf("&&& alpha channel set!\n"));
  336.                     }
  337.                     else
  338.                     {
  339.                         DB(kprintf("&&& error: alpha channel not set!!\n"));
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.  
  345.  
  346.         if (p == NULL)        //err
  347.         {
  348.             if (pic.array)
  349.             {
  350.                 FreeRenderVec(pic.array);
  351.             }
  352.         
  353.             if (pic.palette)
  354.             {
  355.                 DeletePalette(pic.palette);
  356.             }
  357.         }
  358.         
  359.  
  360.         if (alphaarray)
  361.         {
  362.             FreeRenderVec(alphaarray);
  363.         }
  364.  
  365.  
  366.         if (errptr)
  367.         {
  368.             *errptr = err;
  369.         }
  370.     
  371.         return p;
  372.     }
  373. }
  374.  
  375.  
  376.  
  377.  
  378. #if 0
  379.  
  380. /*--------------------------------------------------------------------
  381.  
  382.     isjpeg = DetectJPEG(filename)
  383.  
  384. --------------------------------------------------------------------*/
  385.  
  386. BOOL DetectJPEG(STRPTR filename)
  387. {
  388.     BOOL jpeg = FALSE;
  389.     BPTR fh;
  390.     APTR obj;
  391.  
  392.     if (TowerBase)
  393.     {
  394.         if (fh = Open(filename, MODE_OLDFILE))
  395.         {
  396.             if (obj = NewExtObject(JPEGCODEC, CDA_Coding, FALSE,
  397.                 CDA_StreamType, CDST_FILE, CDA_Stream, fh,
  398.                 TAG_DONE))
  399.             {
  400.                 if (DoMethod(obj, CDM_READHEADER) == HEADER_READY)
  401.                 {
  402.                     jpeg = TRUE;
  403.                 }
  404.  
  405.                 DisposeExtObject(obj);            
  406.             }
  407.             Close(fh);
  408.         }        
  409.     }
  410.  
  411.     return jpeg;
  412. }
  413.  
  414.  
  415. /*--------------------------------------------------------------------
  416.  
  417.     pic = LoadJPEG(filename, tags)
  418.  
  419. --------------------------------------------------------------------*/
  420.  
  421. PIC *LoadJPEG(STRPTR filename, TAGLIST tags)
  422. {
  423.     PIC *pic = NULL;
  424.  
  425.     if (TowerBase && IntuitionBase)
  426.     {
  427.  
  428.         BPTR fh;
  429.         APTR obj;
  430.         ULONG colorspace;
  431.         ULONG pixelformat = 0, width = 0, height = 0, components;
  432.         char *linebuffer;
  433.         LONG err;
  434.         int x, y;
  435.         char *buffer = NULL;
  436.  
  437.         ULONG hstype = GetTagData(GGFX_HSType, HSTYPE_UNDEFINED, tags);
  438.         LONG *errptr = (LONG *) GetTagData(GGFX_ErrorCode, NULL, tags);
  439.  
  440.         if (fh = Open(filename, MODE_OLDFILE))
  441.         {
  442.             if (obj = NewExtObject(JPEGCODEC, CDA_Coding, FALSE,
  443.                 CDA_StreamType, CDST_FILE, CDA_Stream, fh,
  444.                 TAG_DONE))
  445.             {
  446.                 if (DoMethod(obj, CDM_READHEADER) == HEADER_READY)
  447.                 {
  448.                     if (GetAttr(PCDA_ColorSpace, obj, &colorspace))
  449.                     {
  450.                         if (colorspace == PCDCS_GRAYSCALE)
  451.                         {
  452.                             pixelformat = PIXFMT_CHUNKY_CLUT;
  453.                         }
  454.                         else
  455.                         {
  456.                             SetAttrs(obj, PCDA_ColorSpace, PCDCS_RGB, TAG_DONE);
  457.                             pixelformat = PIXFMT_0RGB_32;
  458.                         }
  459.  
  460.                         if (DoMethod(obj, CDM_START))
  461.                         {
  462.                             if (GetAttr(PCDA_Width, obj, &width) &&
  463.                                 GetAttr(PCDA_Height, obj, &height) &&
  464.                                 GetAttr(PCDA_Components, obj, &components))
  465.                             {
  466.                                 if (components == 3 || components == 1)
  467.                                 {
  468.                                     err = 0;
  469.  
  470.                                     if (buffer = AllocRenderVec(MemHandler, PIXELSIZE(pixelformat) * width * height))
  471.                                     {
  472.                                         char *bufp = buffer;
  473.                             
  474.                                         if (pixelformat == PIXFMT_CHUNKY_CLUT)
  475.                                         {
  476.                                             for (y = 0; y < height; ++y)
  477.                                             {
  478.                                                 if (!DoMethod(obj, CDM_PROCESS, bufp, width))
  479.                                                 {
  480.                                                     if (!(err = IoErr())) err = ERROR_NO_FREE_STORE;
  481.                                                     break;
  482.                                                 }
  483.                                                 bufp += width;
  484.                                             }
  485.                                         }
  486.                                         else
  487.                                         {
  488.                                             if (linebuffer = AllocRenderVec(MemHandler, width * 3))
  489.                                             {
  490.                                                 for (y = 0; y < height; ++y)
  491.                                                 {
  492.                                                     if (!DoMethod(obj, CDM_PROCESS, linebuffer, width * 3))
  493.                                                     {
  494.                                                         if (!(err = IoErr())) err = ERROR_NO_FREE_STORE;
  495.                                                         break;
  496.                                                     }
  497.                                                     
  498.                                                     for (x = 0; x < width; ++x)
  499.                                                     {
  500.                                                         bufp[0] = 0;
  501.                                                         bufp[1] = linebuffer[x * 3];
  502.                                                         bufp[2] = linebuffer[x * 3 + 1];
  503.                                                         bufp[3] = linebuffer[x * 3 + 2];
  504.                                                         bufp += 4;
  505.                                                     }
  506.                                                 }
  507.                                                 FreeRenderVec(linebuffer);
  508.                                             }
  509.                                             else err = ERROR_NO_FREE_STORE;
  510.                                         }
  511.  
  512.                                     }
  513.                                     else err = ERROR_NO_FREE_STORE;
  514.  
  515.                                 }
  516.                                 else err = ERROR_NOT_IMPLEMENTED;
  517.                             }
  518.                             else err = ERROR_REQUIRED_ARG_MISSING;
  519.                         }
  520.                         else err = ERROR_NOT_IMPLEMENTED;
  521.                     }
  522.                     else err = ERROR_NO_FREE_STORE;
  523.                 }
  524.                 else err = ERROR_NO_FREE_STORE;
  525.                 
  526.                 DisposeExtObject(obj);            
  527.             }
  528.             else if (!(err = IoErr())) err = ERROR_NO_FREE_STORE;
  529.  
  530.             Close(fh);
  531.         }
  532.         else if (!(err = IoErr())) err = ERROR_NO_FREE_STORE;
  533.  
  534.  
  535.         if (err)
  536.         {
  537.             FreeRenderVec(buffer);
  538.         }
  539.         else
  540.         {
  541.             if (buffer && width && height)
  542.             {
  543.                 if (!(pic = MakePicture(buffer, width, height,
  544.                     GGFX_HSType, hstype,
  545.                     GGFX_PixelFormat, pixelformat,
  546.                     GGFX_Owner, TRUE,
  547.                     TAG_DONE)))
  548.                 {
  549.                     err = ERROR_NO_FREE_STORE;
  550.                 }
  551.             }
  552.             else
  553.             {
  554.                 FreeRenderVec(buffer);
  555.             }
  556.         }
  557.         
  558.         if (errptr)
  559.         {
  560.             *errptr = err;
  561.         }
  562.  
  563.     }
  564.  
  565.     return pic;
  566. }
  567.  
  568. #endif
  569.  
  570.  
  571. /*********************************************************************
  572. ----------------------------------------------------------------------
  573.  
  574.     BOOL IsPicture ( filename, tags )
  575.  
  576.     GGFX_FormatName
  577.  
  578. ----------------------------------------------------------------------
  579. *********************************************************************/
  580.  
  581. BOOL SAVE_DS ASM IsPictureA(REG(a0) char *file_name, REG(a1) TAGLIST tags)
  582. {
  583.     struct DataType *dtn;
  584.     struct DataTypeHeader *dth;
  585.     BPTR lock;
  586.     BOOL it_is = FALSE;
  587.  
  588.  
  589.     if (DataTypesBase)
  590.     {
  591.        if (lock = Lock(file_name, ACCESS_READ))
  592.        {
  593.           if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
  594.           {
  595.              dth = dtn->dtn_Header;
  596.              if (dth->dth_GroupID == GID_PICTURE)   /* is it a picture? */
  597.              {
  598.                 char **formatname = (char **) GetTagData(GGFX_FormatName, NULL, tags);
  599.  
  600.                 it_is = TRUE;
  601.     
  602.                 if (formatname)
  603.                 {
  604.                     int l = strlen(dth->dth_Name);
  605.                     if (l > 0)
  606.                     {
  607.                         *formatname = AllocVec(l + 1, MEMF_ANY);
  608.                         if (*formatname)
  609.                         {
  610.                             strcpy(*formatname, dth->dth_Name);
  611.                         }
  612.                     }
  613.                 }
  614.              }
  615.  
  616.              ReleaseDataType(dtn);
  617.           }
  618.           UnLock(lock);
  619.        }
  620.     }
  621.     
  622.     return(it_is);
  623. }
  624.  
  625.  
  626.  
  627.  
  628. /*********************************************************************
  629. ----------------------------------------------------------------------
  630.  
  631.     picture = LoadPicture ( filename, tags )
  632.  
  633.     GGFX_ErrorCode
  634.     GGFX_HSType
  635.     GGFX_License
  636.  
  637. ----------------------------------------------------------------------
  638. *********************************************************************/
  639.  
  640. PIC SAVE_DS ASM *LoadPictureA(REG(a0) STRPTR filename, REG(a1) TAGLIST tags)
  641. {
  642.     PIC *pic = NULL;
  643.  
  644.  
  645.     #if 0
  646.     if (GetTagData(GGFX_License, FALSE, tags))
  647.     {
  648.         if (DetectJPEG(filename))
  649.         {
  650.             DB(kprintf("using JPEG directly!\n"));
  651.             pic = LoadJPEG(filename, tags);
  652.         }
  653.     }
  654.     #endif
  655.     
  656.  
  657.     if (!pic)
  658.     {
  659.         pic = LoadPictureDT(filename, tags);
  660.     }
  661.  
  662.     return pic;
  663. }
  664.  
  665.